package com.hylanda.pb.controller.proxy;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.hylanda.pb.entity.ApiResponse;
import com.hylanda.pb.entity.ConnectionString;
import com.hylanda.pb.service.ApiService;

/**
 * @author zhangy
 * @E-mail:blackoon88@gmail.com
 * @qq:846579287
 * @version created at：2017年11月30日 下午5:38:25 note
 */
@RestController
@RequestMapping("/api")
public class ApiController {

	@Autowired
	private ApiService apiService;
	
	@RequestMapping(value = { "/upload/powerbireport/{username}/{password}" }, method = RequestMethod.POST)
	public ApiResponse uploadReportFile(HttpServletRequest request,
			@RequestPart("file") Part file,@PathVariable String username,@PathVariable String password) {
		boolean rq1 = apiService.uploadCatalogItem(file, null,username,password);
		ApiResponse apiResponse = new ApiResponse();
		
		if (!rq1) {
			apiResponse.setInfo("上传报表接口失败");
			apiResponse.setStatus("fail");
		}
		return apiResponse;
	}

	
	@RequestMapping(value = { "/create/folder" }, method = RequestMethod.GET)
	public ApiResponse createFolder(HttpServletRequest request, @RequestParam String parentName){
		ApiResponse apiResponse=apiService.createFolder(parentName);
		return apiResponse;
	}
	
	@RequestMapping(value={"/patch/datasource"},method=RequestMethod.POST)
	public ApiResponse patchDataSource(HttpServletRequest request, @RequestParam String path,@RequestBody String json){
		ConnectionString connectionString=null;
		try {
			connectionString=JSON.parseObject(json, ConnectionString.class);
		} catch (Exception e) {
		}
		if(connectionString==null) return new ApiResponse("请求body参数不正确","fail");
		
		ApiResponse apiResponse=apiService.patchDataSource(path,connectionString);
		return apiResponse;
	}
	@RequestMapping(value={"/patch/datasource/test"},method=RequestMethod.POST)
	public ApiResponse patchDataSourceTest(HttpServletRequest request, @RequestParam String path,@RequestBody String json){
		
		ApiResponse apiResponse=apiService.patchDataSource2(path,json);
		return apiResponse;
	}
//	@RequestMapping(value={"/query/datasource"},method=RequestMethod.GET)
//	public ApiResponse querydatasource(HttpServletRequest request, @RequestParam String path){
//		ApiResponse apiResponse=apiService.queryDataSource(path);
//		return  apiResponse;
//	}
//	
//	@RequestMapping(value="/create/datasource")
//	public ApiResponse createDatasource(){
//		ApiResponse apiResponse=apiService.createDataSource();
//		return apiResponse;
//	}
}
